package iscript.coreFramework.data.datatree 
{
	import flash.utils.Dictionary;
	import iscript.coreFramework.data.datatable.DataTable;
	import iscript.coreFramework.util.GUID;
	/**
	 * ...
	 * @author ...
	 */
	public class DataTreeNode 
	{
		public function DataTreeNode() 
		{
				this._id = "root";
		}
		
		private var _id:String;	
		private var _parentNodeId:String;			
		private var _userData:Object;
		private var _treeData:DataTable;
		
		public function get  treeData():DataTable {
			 return _treeData;
			}
		
		public function set  treeData(value:DataTable):void {
			  _treeData = value;
			}	
			
		public function add(node:DataTreeNode):void {
			      var _data:Dictionary = new Dictionary();
			      _data["id"] = GUID.create();
			      _data["parentNodeId"] = this.Id;
				  _data["treeNode"] = node;				  
				  node._id = _data["id"] ;
				  node.treeData = this.treeData;
				  node._parentNodeId = this.Id;
				  this.treeData.addRow(_data);
			}
			
		/**
		 * 会自动移除所有子栏目
		 */
		public function remove():void {	
			if (this.IsRootNode) throw new Error("根目录不能移除");
			var targetIndex:Vector.<int> = new Vector.<int>();
			 var sourceRow:Vector.<Dictionary> = _treeData.Rows;
			 for (var i:int = 0; i <sourceRow.length; i++)
			 {
				if (this.contain(DataTreeNode(sourceRow[i]["treeNode"]))) {
					   targetIndex.push(i);
					}
			 }
			 targetIndex.reverse();
			 for (var j:int = 0; j <targetIndex.length; j++) 
			 {
				_treeData.removeRowAt(targetIndex[j]);		
			 }
			}
			
		public function get childNodes():Vector.<DataTreeNode> {
			   var nodes:Vector.<DataTreeNode> = new Vector.<DataTreeNode>();
			   var _data:Vector.<Dictionary> = this._treeData.search( {
													 parentNodeId:{"=":this._id}
													});
			   for (var i:int = 0; i < _data.length; i++) 
			   {
				   nodes.push( _data[i]["treeNode"]);
			   }
			   return nodes;
			}
		
		public function searchNode($filterdata:Object):Vector.<DataTreeNode> {
			return null;
			}
		 
		public function get IsRootNode():Boolean {
			  return this._id == "root";
			} 
		 
		public function set UserData(value:Object):void {
			  _userData = value;
			}		
		
		public function get UserData():Object {
			  return this._userData;
			}		
			
		public function get Id():String {
			return this._id;
			}	
			
		public function get parentNodeId():String {
			return _parentNodeId;
			}
		 
		public function get parentNode():DataTreeNode {
			   var _data:Vector.<Dictionary> = this._treeData.search( {
													 id:{"=":parentNodeId}
													});	
			   if (_data.length == 0) return null;
			   return _data[0]["treeNode"];			 
			}
	
		public function toRootPath():DataTreeNodePath {				
			    var path:DataTreeNodePath = new DataTreeNodePath();
				var node:DataTreeNode = this;
				    path.add(this);			
				while (node.parentNode != null) {
						 path.add(node.parentNode);
						 node = node.parentNode;
					}
				return path;
		    }
			
		public function	 contain(childNode:DataTreeNode):Boolean {
			    var path:Vector.<DataTreeNode> = childNode.toRootPath().Nodes;
				for (var i:int = 0; i < path.length; i++) 
				 {
					if (path[i] == this) { return true; }
				 }
			     return false;
			}
			
	}

}